home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / SPR5 / CONVERT / SPR2TGE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-27  |  7.4 KB  |  272 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <alloc.h>
  6. #include <dir.h>
  7. #include "d:\tge\tge.h"
  8.  
  9. /*
  10. ╔═════════════════════════════════════════════════════════════════╗
  11. ║             █▓▒░ WordUp Graphics Toolkit V4.0 ░▒▓█              ║
  12. ║ Conversion Utility   Copyright 1994 WordUp Software Productions ║
  13. ╟─────────────────────────────────────────────────────────────────╢
  14. ║ Program:      spr2tge.c                                         ║
  15. ║ Description:  Utility for converting a WGT sprite file to       ║
  16. ║               The Graphics Engine (by Matthew Hildebrand)       ║
  17. ║                                                                 ║
  18. ║ Written by:   Chris Egerter                                     ║
  19. ╚═════════════════════════════════════════════════════════════════╝
  20. */
  21.  
  22.  
  23. void far *sprites[2001];
  24. void convertsprites(char *,char *);
  25. void loadsprites(char *,void far *loadspr[]);
  26. void testsprites();
  27. void freesprites(void far *freespr[]);
  28. void forceExtension(char *filename, char *extention);
  29. int maxx,maxy,colours;
  30.  
  31.  
  32. void main (int argc, char *argv[])
  33. {
  34.   if (argc < 2)                /* Parse command line */
  35.   {
  36.     printf("WGT Sprite File to TGE converter\n\nUsage:  spr2tge driver\n\n");
  37.     exit(1);
  38.   }
  39.  
  40.   forceExtension(argv[1], ".DRV");    /* Load the specified driver */
  41.   if (!loadGraphDriver(argv[1]))
  42.   {
  43.     printf("Error loading \"%s\".\n\n", argv[1]);
  44.     exit(1);
  45.   }
  46.  
  47.   maxx = _grSystemDrv->maxx+1;        /* set up variables */
  48.   maxy = _grSystemDrv->maxy+1;
  49.   colours = _grSystemDrv->colours+1;
  50.  
  51.   if (!initGraphics())            /* initialize graphics mode */
  52.   {
  53.     printf("Unable to initialize graphics hardware.\n\n");
  54.     exit(1);
  55.   }
  56.  
  57.    convertsprites("sprtconv.spr","out.spr");
  58.    loadsprites("out.spr",sprites);
  59.    testsprites();
  60.    freesprites(sprites);
  61.  
  62.  
  63.    /* clean up */
  64.   deInitGraphics();            /* restore text and quit */
  65.   unloadGraphDriver();
  66. }
  67.  
  68. void forceExtension(char *filename, char *extention)
  69. {
  70.   char fileDrive[MAXDRIVE];
  71.   char fileDir[MAXDIR];
  72.   char fileFile[MAXFILE];
  73.   char fileExt[MAXEXT];
  74.  
  75.   fnsplit(filename, fileDrive, fileDir, fileFile, fileExt);
  76.  
  77.   if (strcmpi(fileExt, extention))
  78.     fnmerge(filename, fileDrive, fileDir, fileFile, extention);
  79. }
  80.  
  81. void convertsprites(char *infile,char *outfile)
  82. {
  83. FILE *in;   /* 256 color sprite file */
  84. FILE *out;  /* converted sprite file */
  85. unsigned char palette[768];         /* 256 * 3 (RGB values) */
  86. int maxcolor;
  87. int maxsprite;
  88. long size;
  89.  
  90. unsigned char far *temp;
  91. int a,b,i,spritemade;
  92. char buf[14];
  93. int x,y;
  94. int startingsprite;
  95.  
  96.     /* Open the files */
  97.     if ((in = fopen (infile, "rb")) == NULL)
  98.        {
  99.        deInitGraphics();            /* restore text and quit */
  100.        unloadGraphDriver();
  101.        printf("Could not open 256 color sprite file");
  102.        exit(1);
  103.        }
  104.     if ((out = fopen (outfile, "wb")) == NULL)
  105.        {
  106.        deInitGraphics();            /* restore text and quit */
  107.        unloadGraphDriver();
  108.        printf("Could not open converted sprite file");
  109.        exit(1);
  110.        }
  111.  
  112.  fread(&a, 1, 2, in);
  113.      /* Get the version number, and change the startingsprite accordingly.
  114.      If version <= 3, maxsprite contains the maximum number of sprites
  115.      that can be stored in a file.  If version > 4, maxsprite contains
  116.      the number of the highest sprite in the file. (empty sprites at
  117.      the end are not kept in the file. */
  118.  if (a <= 3)
  119.    startingsprite = 1;
  120.  else startingsprite = 0;    /* Version 4 starts at sprite 0 */
  121.  
  122.  
  123.     fread (buf, 1, 13, in); /* sprite header */
  124.     if (0 == strnicmp (" Sprite File ", buf, 13)) /* see if it is a sprite file */
  125.     {
  126.     fread(palette,1,768,in); /* Read in 256 color palette */
  127.     maxcolor=colours;
  128.         fwrite(&maxcolor, 1, 2, out);
  129.         /* Write the number of colors stored in file */
  130.  
  131.     for (i = 0; i < maxcolor; i++) /* read in the palette */
  132.         {
  133.        fputc(palette[i*3],out);    /* Write out Red */
  134.        fputc(palette[i*3+1],out);  /* Green */
  135.        fputc(palette[i*3+2],out);  /* And Blue color values */
  136.  
  137.         }
  138.         
  139.         fread(&maxsprite, 1, 2, in); /* maximum sprites in this file */
  140.         fwrite(&maxsprite, 1, 2, out);
  141.     for (i = startingsprite; i <= maxsprite; i++) /* load them in */
  142.     {
  143.             fread(&spritemade, 1, 2, in); /* flag to see if sprite exists */
  144.             fwrite(&spritemade, 1, 2, out);
  145.         if (spritemade == 1)
  146.         {
  147.         filledRect(0,0,319,199,0);  /* maximum sprite size */
  148.                 fread(&a, 1, 2, in); /* get width and height */
  149.                 fread(&b, 1, 2, in);
  150.                 fwrite(&a, 1, 2, out); /* put width and height */
  151.                 fwrite(&b, 1, 2, out);
  152.  
  153.         /* Read in the image data. Each byte represents a color
  154.         from 0-255. Obviously converting sprites that use more colors
  155.         than the current mode allows will not work. Draw sprites using
  156.         only the first colors (eg 0-16), up to maxcolors of the mode you're using. */
  157.         for (y=0; y<b; y++)
  158.           for (x=0; x<a; x++)
  159.              putPixel(x,y,fgetc(in));
  160.  
  161.  
  162.         size = imageSize(0, 0, a-1, b-1); /* get byte size of image */
  163.         if ((temp = farmalloc(size)) == NULL)
  164.             {
  165.               deInitGraphics();
  166.               unloadGraphDriver();
  167.               printf("Error: not enough heap space in convertsprites.\n");
  168.               exit(1);
  169.            }
  170.         getImage(0, 0, a-1, b-1,temp); /* Get the image in new mode */
  171.         fwrite(temp,size,1,out); /* Write the data in getimage format */
  172.         farfree(temp);
  173.         }
  174.         }
  175.     }
  176.     fclose (in);
  177.     fclose (out);
  178. }
  179.  
  180. void loadsprites(char *infile,void far *loadspr[])
  181. {
  182. FILE *in;   /* converted color sprite file */
  183. int maxsprite;
  184. unsigned char pal[768];
  185. int maxcolor;
  186. long size;
  187.  
  188. int a,b,i,spritemade;
  189.  
  190.     /* Open the files */
  191.     if ((in = fopen (infile, "rb")) == NULL)
  192.        {
  193.        deInitGraphics();            /* restore text and quit */
  194.        unloadGraphDriver();
  195.        printf("Could not load converted color sprite file");
  196.        exit(1);
  197.        }
  198.  
  199.     fread(&maxcolor, 1, 2, in);
  200.  
  201.     fread(pal,3*maxcolor,1,in);
  202.     for (i = 0; i < maxcolor*3; i++)
  203.        pal[i]*=4;
  204.     /* We need to multiply the colour values by 4 since WGT has an
  205.        intensity range of 0-64 while TGE uses 0-255. */
  206.  
  207.     setBlockPalette(0,256,pal);
  208.     
  209.     fread (&maxsprite, 1, 2, in);
  210.            /* maximum sprites in this file */
  211.  
  212.     for (i = 0; i < maxsprite; i++) /* load them in */
  213.     {
  214.         fread (&spritemade, 1, 2, in);
  215.              /* flag to see if sprite exists */
  216.  
  217.         if (spritemade == 1)
  218.         {
  219.         fread(&a, 1, 2, in); /* get width and height */
  220.         fread(&, 1, 2, in);
  221.  
  222.  
  223.         size = imageSize(0, 0, a-1, b-1); /* get byte size of image */
  224.         if ((loadspr[i] = farmalloc(size)) == NULL)
  225.             {
  226.               deInitGraphics();            /* restore text and quit */
  227.               unloadGraphDriver();
  228.               printf("Error: not enough heap space in loadsprites().\n");
  229.               freesprites(loadspr);
  230.               exit(1);
  231.            }
  232.         fread(loadspr[i],size,1,in);
  233.         } 
  234.         else loadspr[i]=NULL;
  235.     
  236.     }
  237.     fclose(in);
  238. }
  239.  
  240.  
  241.  
  242.  
  243. void testsprites(void)
  244. /* Loops through 10 sprites, displaying them on the screen
  245.    using the putimage method. Press a key to go to the next sprite. */
  246. {
  247. int i,j;
  248.  
  249. for (i=0; i<10; i++) 
  250.   {
  251.   clearGraphics(0);
  252.   
  253.    if (sprites[i] !=NULL)
  254.      {
  255.      for (j=1; j<20; j++)
  256.        putImage(rand() % maxx,rand() % maxy,sprites[i]);
  257.        /* Put the converted image on the screen */
  258.      getch();
  259.      }
  260.   }
  261. }
  262.  
  263. void freesprites(void far *freespr[])
  264. {
  265. int i;
  266.  
  267. for (i = 0; i < 2001; i++)
  268.   {
  269.   if (freespr[i] !=NULL)
  270.     farfree(freespr[i]);
  271.   }
  272. }